package com.wlyy.starter.mybatis.expand;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

public class SelectLimitOne extends AbstractMethod {
    public final static String METHOD_ID = "selectLimitOne";

    public SelectLimitOne(String name) {
        super(name);
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        final String sqlTemp = "<script>select %s from %s ${ew.customSqlSegment} limit 1 </script>";
        final String tableName = tableInfo.getTableName();
        String sql = String.format(sqlTemp, sqlSelectColumns(tableInfo, true), tableName);
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return this.addSelectMappedStatementForTable(mapperClass, METHOD_ID, sqlSource, tableInfo);
    }
}
